<?php

namespace App\Service;

use Illuminate\Support\Facades\Storage;

class FileUploadService
{


    //单个上传
    public function uploadFile($folder)
    {
        // 检查是否有文件上传
        if (isset($_FILES['file'])) {
            $file = $_FILES['file'];
            // dump($file);die;
            // 检查上传错误
            if ($file['error'] === UPLOAD_ERR_OK) {
                $folder=$folder.'/'.date("Y").'/'.date("m").'/'.date("d").'/';
                if (!file_exists($folder)) {
                    mkdir($folder, 0777, true);
                }
                $filename = time() . '_' . $file['name'];                
                move_uploaded_file($file['tmp_name'], $folder . $filename);
            }
        }
        return $folder.$filename;
    }


    //批量上传
    public function uploadFiles($folder)
    {

        $arrPath=[];
        // 检查是否有文件上传
        if (isset($_FILES['file'])) {
            $file = $_FILES['file'];
            // dump($file);die;
            $length=count($_FILES['file']['name']);
            $folder=$folder.'/'.date("Y").'/'.date("m").'/'.date("d").'/';
            if (!file_exists($folder)) {
                mkdir($folder, 0777, true);
            }
            
            for ($i=0; $i <$length; $i++) { 
                // dump($file['name']);die;
                if($file['error'][$i]=== UPLOAD_ERR_OK){
                    $filename = time() . '_' . $file['name'][$i];
                    // 移动上传的文件到目标位置
                    if (move_uploaded_file($file['tmp_name'][$i], $folder . $filename)) {
                        array_push($arrPath,$folder.$filename);
                    }
                }else{
                    dump($file['error'][$i]);die;
                }
            }
        }
        return $arrPath;
    }
}
